<?php

namespace App\Services\Assess;

use App\Models\Account\UserAccountModel;
use App\Models\Assess\TagManageModel;
use App\Models\UserModel;
use App\Services\BaseService;
use App\Services\Common\RedisService;
use Illuminate\Support\Facades\Log;

class TagManageService extends BaseService
{

    //标签用途(1:激活买家管理,2:其他)
    public $tagType = [
        1=>'订单与客户',
        2=>'通用'
    ];

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new TagManageModel();
    }


    public function getList()
    {
        $param = request()->all();
        // 查询条件
        $map[] = ['mark','=',1];
        $tagName = getter($param, 'tag_name', '');
        $tagType = getter($param, 'tag_type', 0);
        $createUser = getter($param, 'create_user', 0);
        $sort = getter($param, 'sort', 'create_time');
        $order = getter($param, 'order', 'desc');
        $time = getter($param, 'time', []);
        $page_size = getter($param, 'page', 1);
        $limit = getter($param, 'limit', 10);
        if (!empty($time)) {
            if (!empty($time[0])) $map[] = ['create_time', '>=', strtotime($time[0])];
            if (!empty($time[1])) $map[] = ['create_time', '<=', strtotime($time[1])];
        }
        if (!empty($tagName))  $map[] = ['tag_name', 'like', "%{$tagName}%"];
        if (!empty($tagType))  $map[] = ['tag_type', '=', $tagType];
        if (!empty($createUser))  $map[] = ['create_user', '=', $createUser];


        //权限过滤 组员只能看自己的 组长可以看所有的
        $createUserList = [];

        $model2 = $this->model->where($map);
        if (!empty($createUserList) && empty($createUser)) $model2->whereIn('create_user',$createUserList);
        $count = $model2->count();
        if (!empty($createUserList) && empty($createUser)) $map[] = ['create_user', 'in', $createUserList];

        $offset = ($page_size - 1) * $limit;
        $list = $this->model->getList($map, [[$sort, $order]],"{$offset},{$limit}");


        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
    }


    public function getTagManageConfig()
    {
        $config = [];
        $config['tag_type'] = $this->tagType ;
        //获取用户信息
        $userData = RedisService::getUserList();
        $config['user_list'] = $userData;

        return message("保存成功", true, $config);

    }


    public function getTagList()
    {
        $tagList = [];
        $TagManageModel = new TagManageModel();
        $tagManageList= $TagManageModel->where('status','=',1)->where('mark','=',1)->get()->toArray();
        $icon = [
            ['title'=>'标签','icons'=>$tagManageList],
        ];
        return message("保存成功", true, $icon);
    }



    /**
     * @Notes:添加或编辑
     * @Interface edit
     * @return array|void
     * @author: wuyu
     * @Time: 2023/5/8   16:45
     */
    public function edit()
    {
        try {
            $params = request()->all();
            $id = $params['id'] ?? 0;
            $tagColor = $params['tag_color'] ?? '';
            $tagName = $params['tag_name'] ?? '';
            $tagType = $params['tag_type'] ?? 1;

            if (empty($tagColor)) throw new \Exception('标签样式不能为空');
            if (empty($tagName)) throw new \Exception('标签名称不能为空');
            if (empty($tagType)) throw new \Exception('标签用途不能为空');

            $data = [
                'tag_name' => $tagName,
                'tag_color' => $tagColor,
                'tag_type' => $tagType,
            ];
            if (empty($id)) {
                $data['create_user'] = $this->model->userId;
                $data['create_time'] = $data['update_time'] = time();
                $exists = $this->model->where('tag_name', '=', $tagName)->where('mark', '=', 1)->count();
            } else {
                $data['update_user'] = $this->model->userId;
                $data['update_time'] = time();
                $exists = $this->model->where('tag_name', '=', $tagName)->where("id", "<>", $id)->where('mark', '=', 1)->count();
            }
            if ($exists > 0) throw new \Exception('标签名称已存在,请勿重复添加');

            if (empty($id)) {
                $insertId = $this->model->insertGetId($data);
            } else {
                $insertId = $this->model->where('id', '=', $id)->update($data);
                $this->model->cacheReset($id, $data, $id);
            }
            if (empty($insertId)) throw new \Exception('操作失败');

            return message("保存成功", true, []);

        } catch (\Exception $e) {
            return message("保存失败:" . $e->getMessage(), false, []);
        }
    }


    public function status()
    {

        $params = request()->all();

        $id = $params['id'] ?? 0;
        $status = $params['status'] ?? 0;
        if (empty($id) || empty($status)) {
            return message("参数不存在", false, []);
        }
        $data = ['status' => $status];
        $this->model->where('id', $id)->update($data);
        $this->model->cacheReset($id, $data, $id);
        return message("操作成功", true, []);
    }

}
